//
//  XinwenController.m
//  网易新闻
//
//  Created by dllo on 15/9/14.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "XinwenController.h"
#import "H24Controller.h"
#import "XWCollectionCell.h"
#import "DetailsController.h"
#import "DetailsController_img.h"
#import "AddressModel.h"
#import "SDImageCache.h"
#import "SearchController.h"
#import "Settings.h"
#import "XWmenu.h"
@interface XinwenController ()<UICollectionViewDataSource, UICollectionViewDelegate, XinwenControllerDelegate>
@property(nonatomic, retain)UILabel *naviTitle;
@property(nonatomic, retain)UIButton *h24;
@property(nonatomic, retain)UIButton *setting;
@property(nonatomic, retain)XWmenu *menu;
@property(nonatomic, retain)UICollectionView *collection;
@property(nonatomic, retain)NSMutableArray *tagArr_select;
@property(nonatomic, retain)NSMutableArray *tagArr_other;
@property(nonatomic, retain)UIScrollView *tagTitle;
@property(nonatomic, retain)UIView *underLine;
@property(nonatomic, retain)NSMutableArray *buttonArr;
@property(nonatomic, retain)DetailsController *details;
@property(nonatomic, retain)DetailsController_img *details_img;
@end

@implementation XinwenController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self handleData];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.alpha = 1;
    self.tabBarController.tabBar.hidden = NO;
    self.h24.hidden = NO;
    self.setting.hidden = NO;
    [self.naviTitle setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.naviTitle setHidden:YES];
    self.setting.hidden = YES;
    self.h24.hidden = YES;
}

- (void)handleData{
    self.tagArr_select = [[NSMutableArray alloc]init];
    self.tagArr_other = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in [[Settings shareSettings]addressArr]) {
        AddressModel *mod = [AddressModel modWithDic:dic];
        if (mod.select) {
            [self.tagArr_select addObject:mod];
        }else{
            [self.tagArr_other addObject:mod];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self notificationHandle];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setTagTitle];
    [self setCollectionView];
    
    self.naviTitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 75, 25)];
    self.naviTitle.textColor = [UIColor whiteColor];
    self.naviTitle.text = @"网易";
    [self.navigationController.navigationBar addSubview:self.naviTitle];
    [self.naviTitle release];
    
    [self createH24Button];
    [self createSetButton];
    [self createMenu];
}

- (void)createH24Button{
    self.h24 = [UIButton buttonWithType:UIButtonTypeSystem];
    self.h24.frame =CGRectMake(295, 10, 35, 25);
    [self.h24 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.h24 setTitle:@"24h" forState:UIControlStateNormal];
    [self.h24 addTarget:self action:@selector(pushH24:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.h24];
}

- (void)createSetButton{
    self.setting = [UIButton buttonWithType:UIButtonTypeSystem];
    self.setting.frame =CGRectMake(340, 10, 25, 25);
    [self.setting setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.setting setTitle:@"···" forState:UIControlStateNormal];
    [self.setting addTarget:self action:@selector(pushMenu:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:self.setting];
}

- (void)createMenu{
    self.menu = [[XWmenu alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.menu.navi = self.navigationController;
    [self.view addSubview:self.menu];
    [self.menu release];
}

- (void)setTagTitle{
    self.tagTitle = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 325, 42)];
    self.tagTitle.contentSize = CGSizeMake(60 * self.tagArr_select.count, 42);
    self.tagTitle.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tagTitle.showsHorizontalScrollIndicator = NO;
    self.tagTitle.showsVerticalScrollIndicator = NO;
    AddressModel *mod = [[AddressModel alloc]init];
    self.buttonArr = [[NSMutableArray alloc]init];
    for (int i=0; i<self.tagArr_select.count; i++) {
        UIButton *tags = [UIButton buttonWithType:UIButtonTypeCustom];
        tags.frame = CGRectMake(60*i, 0, 60, 42);
        mod = self.tagArr_select[i];
        [tags setTitle:mod.name forState:UIControlStateNormal];
        tags.titleLabel.font = [UIFont systemFontOfSize:15];
        [tags setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [tags setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        if (i == 0) {
            tags.selected = YES;
        }
        [self.buttonArr addObject:tags];
        [tags addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.tagTitle addSubview:tags];
    }
    [self.view addSubview:self.tagTitle];
    [self.tagTitle release];
    
    self.underLine = [[UIView alloc]initWithFrame:CGRectMake(0, 40, 60, 2)];
    self.underLine.backgroundColor = [UIColor redColor];
    [self.tagTitle addSubview:self.underLine];
    [self.underLine release];
    [self.buttonArr release];
}

- (void)setCollectionView{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.itemSize = CGSizeMake(375, 520);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 102, 375, 520) collectionViewLayout:flow];
    [flow release];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    self.collection.pagingEnabled = YES;
    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.showsVerticalScrollIndicator = NO;
    self.collection.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;//数据在插入之前页面还没有加载完
    
    [self.collection addObserver:self forKeyPath:@"contentOffset" options:1|2 context:nil];
    
    [self.view addSubview:self.collection];
    [_collection release];
    
    [self.collection registerClass:[XWCollectionCell class] forCellWithReuseIdentifier:@"XWCollectionCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.tagArr_select.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XWCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XWCollectionCell" forIndexPath:indexPath];
    AddressModel *mod = self.tagArr_select[indexPath.item];
    cell.itemURL = mod.address;
    cell.delegate = self;
    return cell;
}

- (void)pushMenu:(UIButton *)button{
    [self.menu popView];
}
- (void)pushH24:(UIButton *)button{
    H24Controller *details = [[H24Controller alloc]init];
    details.desc = @"聚焦今日时事 浓缩新闻精华";
    details.imgname = @"H24Back";
    details.title = @"24小时要闻";
    [self.navigationController pushViewController:details animated:YES];
    [details release];
}
- (void)pushDetailsViewWithDocid:(NSString *)docid{
    self.details = [[DetailsController alloc]init];
    self.details.docid = [[NSString alloc]initWithString:docid];
    [self.navigationController pushViewController:self.details animated:YES];
    [self.details release];
}
- (void)pushDetailsViewWithSkipid:(NSString *)skipid{
    self.details_img = [[DetailsController_img alloc]init];
    NSString *str = [[skipid stringByReplacingOccurrencesOfString:@"|" withString:@"/"]substringFromIndex:4];
    self.details_img.skipID = [[NSString alloc]initWithString:str];
    [self.navigationController pushViewController:self.details_img animated:YES];
    [self.details_img release];
}
- (void)pushSearchView{
    SearchController *search = [[SearchController alloc]init];
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}

- (void)titleClick:(UIButton *)button{
    int page = button.frame.origin.x/60;
        [self.collection setContentOffset: CGPointMake(page*375, 0) animated:YES];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    float con_new = [[change objectForKey:@"new"] CGPointValue].x/375.f;
    float con_old = [[change objectForKey:@"old"] CGPointValue].x/375.f;
    self.underLine.frame = CGRectMake(con_new*60, 40, 60, 2);
    if (self.tagTitle.contentSize.width - con_new*60 <= self.tagTitle.frame.size.width) {
        self.tagTitle.contentOffset = CGPointMake(self.tagTitle.contentSize.width - self.tagTitle.frame.size.width, 0);
    }else{
        if (con_new*60>20) {
            self.tagTitle.contentOffset = CGPointMake(con_new*60-20, 0);
        }else{
            self.tagTitle.contentOffset = CGPointMake(0, 0);
        }
    }
    if ((int)con_new != (int)con_old) {
        for (UIButton *tags in self.buttonArr) {
            tags.selected = NO;
        }
        UIButton *button = self.buttonArr[(int)con_new];
        button.selected = YES;
    }
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.tagTitle.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    [_menu release];
    [_collection removeObserver:self forKeyPath:@"contentOffset"];
    _collection.delegate = nil;
    _collection.dataSource = nil;
    [_collection release];
    [_tagArr_select release];
    [_tagArr_other release];
    [_details release];
    [_details_img release];
    [_tagTitle release];
    [_underLine release];
    [_buttonArr release];
    [_naviTitle release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
